package com.hnkywl.sts.action.kcgl;

import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.kcgl.CxgdService;
import com.hnkywl.sts.service.sys.GnczService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-4-30
 * Time: 上午11:34
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/kcgl/cxgd")
public class CxgdAction extends SimpleActionSupport {
    private CxgdService cxgdService;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //查询条件
    private String khbm;
    private String ckmc;
    private String code;
    private String ggxh;
    private String wzmc;

    private String gdIds;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;


    @Action("cxgd")
    public String cxgd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //撤单集合
    @Action("cxgdList")
    public void cxgdList() {
        this.renderJson(this.cxgdService.findAllCancelGdList(khbm, ckmc, code, ggxh, wzmc, page, rows));
    }

    //撤单实际方法
    @Action("cxgd_cd")
    public void cxgdCd() {
        try {
            List<Map<String, Object>> map = cxgdService.cd(gdIds);
            if (map == null || map.size() == 0) {
                renderText("{\"statusCode\":\"300\", \"message\":\"撤单失败\"}");
            } else {
                StringBuffer Failures = new StringBuffer();
                for (int i = 0; i < map.size(); i++) {
                    String resCode = map.get(i).get("code").toString();
                    switch (Integer.parseInt(resCode)) {
                        case 0:
                            //校验传入的json
                            cxgdService.checkJsonResponse(map);
                            //撤单成功的方法！
                            cxgdService.handleCdKh(map.get(i));
                            Failures.append(cxgdService.tipResponse(map.get(i), "撤牌成功!"));
                            break;
                        case -13:
                            // 相同撤单指令！
                            Failures.append(cxgdService.tipResponse(map.get(i), "相同的撤单指令!"));
                            break;
                        default:
                            //非0撤单失败！
                            Failures.append(cxgdService.tipResponse(map.get(i), map.get(i).get("message") == null ? "撤单失败!" : map.get(i).get("message").toString()));
                    }
                }
                renderText("{\"statusCode\":\"200\", \"message\":\"" + Failures + "\", \"operationType\":\"add\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getKhbm() {
        return khbm;
    }

    public void setKhbm(String khbm) {
        this.khbm = khbm;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCkmc() {
        return ckmc;
    }

    public void setCkmc(String ckmc) {
        this.ckmc = ckmc;
    }

    public String getGgxh() {
        return ggxh;
    }

    public void setGgxh(String ggxh) {
        this.ggxh = ggxh;
    }

    public String getWzmc() {
        return wzmc;
    }

    public void setWzmc(String wzmc) {
        this.wzmc = wzmc;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getGdIds() {
        return gdIds;
    }

    public void setGdIds(String gdIds) {
        this.gdIds = gdIds;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setCxgdService(CxgdService cxgdService) {
        this.cxgdService = cxgdService;
    }
}
